﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XbdSoft.Contract;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Diagnostics;

namespace XbdSoft.Services
{
    public class AutoUpdateService:IAutoUpdateService
    {
        private const string VersionFilePathKey = "versionFilePath";
        private const string InstallFileZipUrlKey = "installFileZipUrl";

        #region Implement IAutoUpdateService Interface

        public string GetLatestVersion()
        {
            var versionFilePath = ConfigurationManager.AppSettings[VersionFilePathKey];
            if (String.IsNullOrEmpty(versionFilePath))
                throw new ArgumentException(String.Format("Don't exist {0} key in configuration file",VersionFilePathKey));
            if (!File.Exists(versionFilePath))
                throw new ArgumentException(String.Format("Exe File: {0} doesn't exist!", versionFilePath));
            try
            {
                FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(versionFilePath);
                return fileVersionInfo.ProductVersion;
            }
            catch { }
            return string.Empty;
        }

        public string GetSetupFileUrl(string verion)
        {
            return ConfigurationManager.AppSettings[InstallFileZipUrlKey];
        }

        public string GetDefaultSetupFileUrl()
        {
            return ConfigurationManager.AppSettings[InstallFileZipUrlKey];
        }
        #endregion


    }
}
